package com.c78.fz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.c78.fz.mapper.HistoryMapper;
import com.c78.fz.mapper.WaterManMapper;
import com.c78.fz.model.entity.History;
import com.c78.fz.service.WaterManService;
import com.c78.fz.common.result.Result;
import com.c78.fz.model.entity.WaterMan;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(rollbackFor = Exception.class)
public class WaterManServiceImpl implements WaterManService {
    @Resource
    WaterManMapper waterManMapper;

    @Resource
    HistoryMapper historyMapper;

    @Override
    public Result getWaterManPage(String name, Integer pageNum, Integer pageSize) {
        Page<WaterMan> page = Page.of(pageNum,pageSize);
        waterManMapper.selectPage(page,name);
        return Result.ok(page);
    }

    @Override
    public Result updateBasicSalary(Integer basicSalary, Integer id) {
        if(waterManMapper.updateBasicSalaryInt(basicSalary,id)>0){
            return Result.ok("调整工资成功");
        }
        return Result.error("调整工资失败");
    }

    @Override
    public Result saveWaterMan(WaterMan waterMan) {
        if(waterManMapper.insert(waterMan)>0){
            return Result.ok("添加送水工成功");
        }
        return Result.error("添加送水工失败");
    }

    @Override
    public Result updateWaterMan(WaterMan waterMan) {
        if(waterManMapper.updateById(waterMan)>0){
            return Result.ok("修改送水工成功");
        }
        return Result.error("修改送水工失败");
    }

    @Override
    public Result getWaterManById(Integer id) {
        LambdaQueryWrapper<WaterMan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WaterMan::getId,id);
        WaterMan waterMan = waterManMapper.selectOne(queryWrapper);
        return Result.ok(waterMan);
    }

    @Override
    public Result getWaterMan() {
        return Result.ok(waterManMapper.selectList(null));
    }

    @Override
    public Result deleteWaterMan(Integer id) {
        LambdaQueryWrapper<WaterMan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WaterMan::getId,id);
        int delete = waterManMapper.delete(queryWrapper);
        if (delete>0){
            LambdaQueryWrapper<History> query = new LambdaQueryWrapper<>();
            query.eq(History::getManId,id);
            historyMapper.delete(query);
        }
        return Result.ok("删除成功");
    }

    @Override
    public Result getSalaryPage(String startTime, String endTime, Integer pageNum, Integer pageSize) {
        Page<WaterMan> page = Page.of(pageNum,pageSize);
        waterManMapper.selectSalaryPage(page,startTime,endTime);
        return Result.ok(page);
    }

    @Override
    public Result getWaterManNo(String startTime, String endTime, Integer pageNum, Integer pageSize) {
        Page<WaterMan> page =Page.of(pageNum,pageSize);
        waterManMapper.selectWaterManNo(page,startTime,endTime);
        return Result.ok(page);
    }

    @Override
    public Result getWaterCount(String name,Integer pageNum,Integer pageSize) {
        Page<WaterMan> page = Page.of(pageNum,pageSize);
        waterManMapper.selectWaterCount(page,name);
//        page.setPages(waterManMapper.selectByManIdPages(name));
        page.setTotal(page.getRecords().size());
        return Result.ok(page);
    }
}
